package radio.vnmedia.vn.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import radio.vnmedia.vn.Defines;
import radio.vnmedia.vn.RadioVnMedia;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class HTMLUtils implements Defines {

	public static long getFileSize(String str_url) throws Exception {
		int size = -1;
		URL url = new URL(str_url);
		URLConnection conn = url.openConnection();
		size = conn.getContentLength();
		System.out.println("File size: " + size);
		conn.getInputStream().close();
		return size;
	}

	public static String convertHtmlCodeToUTF8(String text) throws Exception {
		HTMLRegex mHtmlCharParser = new HTMLRegex(text, "(&#{0,1}[a-zA-Z0-9]{1,4};)");
		while (mHtmlCharParser.next()) {
			text = text.replaceFirst(mHtmlCharParser.getGroup(), RadioVnMedia.HtmlEntity.get(mHtmlCharParser.getGroup()));
			mHtmlCharParser.updateContent(text);
		}
		return text;
	}

	public static Bitmap loadBitmap(String url) {
		Bitmap bitmap = null;
		InputStream in = null;
		BufferedOutputStream out = null;

		try {
			in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

			final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
			out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
			byte[] buffer = new byte[IO_BUFFER_SIZE];
			int len;
			while ((len = in.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			out.flush();

			final byte[] data = dataStream.toByteArray();
			BitmapFactory.Options options = new BitmapFactory.Options();
			// options.inSampleSize = 1;

			bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
		} catch (IOException e) {
			e.printStackTrace();
			Log.e("Phuoc debug", "Could not load Bitmap from: " + url);
		} finally {
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return bitmap;
	}

	public static byte[] getBytesImage(String str_url) throws Exception {
		URL url = new URL(str_url);
		URLConnection con = url.openConnection();
		con.connect();
		InputStream is = con.getInputStream();
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[2000];
		int size;
		DataInputStream dis = new DataInputStream(is);
		while ((size = dis.read(buffer)) != -1) {
			byteStream.write(buffer, 0, size);
		}
		return byteStream.toByteArray();
	}

	public static InputStream getURLInputStream(String str_url) throws Exception {
		URL url = new URL(str_url);
		URLConnection con = url.openConnection();
		con.connect();
		return con.getInputStream();
	}

	public static String downloadHtmlContent(String net_url) throws Exception {
		URL server = new URL(net_url);
		HttpURLConnection connection = (HttpURLConnection) server.openConnection();
		connection.setRequestMethod("GET");
		// connection.setRequestProperty("Content-type","text/html");
		connection.setDoInput(true);
		connection.setDoOutput(true);
		connection.setUseCaches(true);
		connection.addRequestProperty("Accept-Charset", "UTF-8");
		connection.setConnectTimeout(10000);
		connection.connect();
		InputStream is1 = connection.getInputStream();
		BufferedInputStream is = new BufferedInputStream(is1);

		byte[] buffer = new byte[500];
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int flength;
		while ((flength = is.read(buffer)) != -1) {
			baos.write(buffer, 0, flength);
		}
		is.close();
		return new String(baos.toByteArray());
	}
}
